package com.shilan.dllo.wangyi.adpter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.FragmentActivity;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.shilan.dllo.wangyi.R;
import com.shilan.dllo.wangyi.clicklistenerininterface.OnMyClickListener;
import com.shilan.dllo.wangyi.entity.NewsEntity;
import com.shilan.dllo.wangyi.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/30.
 */
public class Headline_recycleview_adpter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


  //定义两个标记 标记加载的view类型
    private static final int TYPE00 = 0;
    private static final int TYPE01 = 1;
    private  List<NewsEntity.T1348647853363Entity> list;
    private RequestQueue queue;//定义一个Volley请求队列
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader.ImageCache imageCache;
    private ImageLoader imageLoader;
    private OnMyClickListener listener;

    public void setOnMyClickListener(OnMyClickListener listener){
        this.listener = listener;
    }


    public void addData(List<NewsEntity.T1348647853363Entity> datas){
        list = datas;
        notifyDataSetChanged();
    }


    public Headline_recycleview_adpter(Context context) {
        queue = Volley.newRequestQueue(context);
    }

    //声明一个布局加载器

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //布局加载器
        LayoutInflater inflater = LayoutInflater.from(parent.getContext()) ;
        switch (viewType) {
            case  TYPE00:
                View view = inflater.inflate(R.layout.head_recycleview_item_a,null);
                return  new ViewHolder00(view);

            case  TYPE01:
               return  new ViewHold01(inflater.inflate(R.layout.head_recycleview_item_b,null));
        }


        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ViewHolder00) {
            ViewHolder00 holder00 = (ViewHolder00) holder; //强制转换类型
            if(list.get(position).getImgextra() == null){
                holder00.AheadTitleTv.setText(list.get(position).getTitle());
                holder00.AheadDigestTv.setText(list.get(position).getDigest());
              getBitmap(list.get(position).getImgsrc(), holder00.AheadImgIv);
            }
            ((ViewHolder00) holder).position = position;
        }else {
            ViewHold01 hold01 = (ViewHold01) holder;
            if (list.get(position).getImgextra() != null) {
                hold01.BheadTitleTv.setText(list.get(position).getTitle());
            }
        }
    }

//    //二级缓存定义的
//public  void getBitmapCache(String url, ImageView imageView){
//    ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,
//            R.mipmap.ic_launcher, R.mipmap.ic_launcher);
//    imageLoader.get(url,listener);
//}
   //通过volley请求请求图片
    public void getBitmap(String url, final ImageView imageView){
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 300, 300, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding", " ");

                return map;
            }
        };
        queue.add(request);
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }

    //定义两个内部类
    public  class  ViewHolder00 extends RecyclerView.ViewHolder implements View.OnClickListener {
    private ImageView AheadImgIv;// 左侧图片
    private TextView AheadTitleTv;// 右侧标题
    private  TextView AheadDigestTv;//右侧摘要
    private RelativeLayout head_item_a;//定义行点击事件
        private int position;


    public ViewHolder00(View itemView) {
        super(itemView);
        AheadImgIv = (ImageView) itemView.findViewById(R.id.AheadImgIv);
        AheadTitleTv = (TextView) itemView.findViewById(R.id.AheadTitleTv);
        AheadDigestTv = (TextView) itemView.findViewById(R.id.AheadDigestTv);
        head_item_a = (RelativeLayout) itemView.findViewById(R.id.head_item_a);
        head_item_a.setOnClickListener(this);
    }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                NewsEntity.T1348647853363Entity entity = list.get(position);
                if (listener != null) {
                    listener.onClickListener(entity);
                }
            }
        }
    }



    public  class  ViewHold01 extends  RecyclerView.ViewHolder{
        private  TextView BheadTitleTv;
        private  ImageView item_image_head01;
        private  ImageView item_image_head02;
        private  ImageView item_image_head03;

        public ViewHold01(View itemView) {
            super(itemView);
            BheadTitleTv = (TextView) itemView.findViewById(R.id.BheadTitleTv);
            item_image_head01 = (ImageView) itemView.findViewById(R.id.item_image_head01);
            item_image_head02 = (ImageView) itemView.findViewById(R.id.item_image_head02);
            item_image_head03 = (ImageView) itemView.findViewById(R.id.item_image_head03);
        }
    }

}
